package com.nifostasky.blog.controller;

import com.nifostasky.blog.utils.MinioNewUtils;
import io.minio.ObjectWriteResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("newMinio")
@RequiredArgsConstructor
public class MinioNewController {

    private final MinioNewUtils minioUtils;

    /**
     * 通过文件名获取url地址
     * @param fileName
     * @return
     */
    @GetMapping
    public String getUrl(String fileName) {
        return minioUtils.getFileUrl(fileName);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping
    public ObjectWriteResponse upload(MultipartFile file) {
        return minioUtils.upload(file);
    }

    /**
     * 下载文件 默认桶 中的文件
     * @param fileName
     * @param response
     */
    @GetMapping("download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) {
        minioUtils.download(fileName, response);
    }

    /**
     * 下载文件 指定桶中的文件
     * @param bucketName
     * @param fileName
     */
    @GetMapping("download/{bucketName}/{fileName}")
    public void download(@PathVariable String bucketName, @PathVariable String fileName, HttpServletResponse response) {
//        minioUtils.download(bucketName, fileName, "/root/minioTest/download" + fileName);
        minioUtils.download(bucketName,fileName,response);
    }

    /**
     * 删除文件
     * @param fileName
     */
    @DeleteMapping
    public void del(String fileName) {
        minioUtils.delFile(fileName);
    }
}
